﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculateResistance
{
    public class Points : List<Point>
    {
        public void AddPoint(Line line)
        {
            Point ps = new Point { Id = line.StartPoint };
            ps.Lines = new Lines();
            ps.Lines.Add(line);
            Point pe = new Point { Id = line.EndPoint };
            pe.Lines = new Lines();
            pe.Lines.Add(line);

            if (!this.Contains(ps))
                this.Add(ps);
            else
                this.Find(p => { return p.Id == ps.Id; }).Lines.Add(line);

            if (!this.Contains(pe))
                this.Add(pe);
            else
                this.Find(p => { return p.Id == pe.Id; }).Lines.Add(line);

        }

        public void AddPoint(List<Line> lines)
        {
            foreach (Line line in lines)
                AddPoint(line);
        }

        public void RemoveLines(List<Line> lines)
        {
            List<Line> ls = new List<Line>(lines);
            foreach (Point p in this)
            {
                if (p.Lines == null) continue;
                p.Lines.RemoveAll(l => ls.Any(tmp => tmp.Id == l.Id));
            }
        }
    }
}
